package sailhero

import (
	"encoding/json"
	"github.com/wsw188440873/ctrl-cali/model"
	"github.com/wsw188440873/ctrl-cali/types"
	"iot-base/common/common"
	"time"
)

func addSubTaskLog(taskId string, groupId string, index int, modelId string, gasType int, Runtime, gasNumber int, flow int, unit string, status int, err error) (int64, error) {
	createAt := model.Datetime(time.Now())
	subLogId, err := common.GetSnowflakeId()
	insertData := model.TaskLogDetails{
		ID:          subLogId,
		TaskId:      taskId,
		TaskGroupId: groupId,
		SubTaskId:   index,
		ModelId:     modelId,
		GasType:     gasType,
		GasNumber:   gasNumber,
		Unit:        unit,
		ErrorMsg:    "",
		Flow:        flow,
		Range:       types.Range[modelId],
		Runtime:     Runtime,
		Event:       types.Start,
		Status:      status,
		CreatedAt:   createAt,
	}
	if err != nil {
		insertData.ErrorMsg = err.Error()
	}
	// 通 0 气 , 没有溶度和单位
	if gasType == 0 {
		insertData.GasNumber = 0
		insertData.Unit = ""
		insertData.Flow = 0
	}
	id, err := model.Dao.TTaskLog.Add(insertData)
	// 添加日志详情
	sendCloudAddTaskLogDetails(insertData)
	return id, err
}

func modifySubTaskLog(id int64, status int, err error, event string, params []float64) error {
	insertData := model.TaskLogDetails{
		Event:    event,
		ErrorMsg: "",
		Status:   status,
	}
	if err != nil {
		insertData.ErrorMsg = err.Error()
	}
	b, _ := json.Marshal(params)
	insertData.ParamValue.UnmarshalJSON(b)
	// 添加日志详情
	err = model.Dao.TTaskLog.Update(id, insertData)
	insertData.ID = id
	sendCloudModifyTaskLogDetails(insertData)
	return err
}
